#include<iostream>
 #include <stdlib.h>
using namespace std;

namespace ns_vector
{
    class vector
    {
        public:
            vector()
                :_nums(nullptr),_size(0),_capacity(0)
            {
            }

            void SeqCheckCapacity()
            {
                if(_size==_capacity)
                {
                    //扩容
                    int newcapacity=_capacity?_capacity*2:4;
                    int * newnums=reinterpret_cast<int*>(realloc(_nums,newcapacity*sizeof(int)));
                    if(!newnums)
                    {
                        cout<<"扩容失败!"<<endl;
                        exit(1);
                    }
                    _nums=newnums;
                    _capacity=newcapacity;
                }
            }

            void insert(int pos,int data)
            {
                if(pos<0&&pos>_size)
                    return ;
                SeqCheckCapacity();

                //将pos后的数据以结尾开始挪动
                for(int i=_size;i>=pos;i--)
                    _nums[i+1]=_nums[i];
                
                //插入
                _nums[pos]=data;
                ++_size;
            }

            void Print()
            {
                for(int i=0;i<=_size;i++)
                {
                    cout<<_nums[i]<<" ";
                }
            }



        private:
            int* _nums;
            int _size;
            int _capacity;
    };

    void test()
    {
        ns_vector::vector v;
        v.insert(0,1);
        v.insert(1,2);

        v.Print();
    }
}


int main()
{
    ns_vector::test();
    return 0;
}